﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Settings;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class TeminalParamterViewModel:ViewModelBase
    {
        public TeminalParamterViewModel()
        {
            AgreementList = new List<string>() { "698.45协议" };
            LogicAddressList = new List<string>() { "00","01","10","11"};
            
            
        }
        private List<string> agreementList;

        public List<string> AgreementList
        {
            get { return agreementList; }
            set { agreementList = value; }
        }

        private int agreementSelected;

        public int AgreementSelected
        {
            get { return agreementSelected; }
            set { agreementSelected = value; }
        }


        private List<string> logicAddressList;

        public List<string> LogicAddressList
        {
            get { return logicAddressList; }
            set { logicAddressList = value; RaisePropertyChanged(); }
        }

        private int logicAddressSelected;

        public int LogicAddressSelected
        {
            get { return logicAddressSelected; }
            set { logicAddressSelected = value; RaisePropertyChanged(); }
        }

        private bool timeTagFlag;

        public bool TimeTagFlag
        {
            get { return timeTagFlag; }
            set { timeTagFlag = value; RaisePropertyChanged(); }
        }

        private string timeTagData;

        public string TimeTagData
        {
            get { return timeTagData; }
            set { timeTagData = value; RaisePropertyChanged(); }
        }

        private List<NormalSetData> tIList;

        public List<NormalSetData> TIList
        {
            get { return tIList; }
            set { tIList = value; }
        }

        public RelayCommand TISetCommand
        {
            get
            {
                var command = new RelayCommand(() =>
                {
                    TIList = new List<NormalSetData> {
                    new NormalSetData{
                        SubItemName="单位",
                        DataValue=int.Parse(TerminalSettings.Instanse.TimeTagData.Substring(0,2)).ToString(),
                        Typename="enum",
                        Example="",
                        Remark="秒(0),分（1),时(2),日(3),月(4),年(5)"
                    },
                    new NormalSetData{
                        SubItemName="间隔值",
                        DataValue=Int32.Parse(TerminalSettings.Instanse.TimeTagData.Substring(2,4)).ToString(),
                        Typename="long-unsigned",
                        Example="若为16进制，需加前缀0x",
                        Remark="间隔值为0表示无间隔"} 
                    };
                    TIParameterView tIParameterView = new TIParameterView();
                    tIParameterView.ShowDialog();
                });
                return command;
            }
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {

                    App.Current.Cfg.Save();
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    // 加载config文件
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    
                    var settings = (TerminalSettings)config.GetSection("terminalSettings");
                    TerminalSettings.Instanse.LogicAddressSelected = settings.LogicAddressSelected;
                    TerminalSettings.Instanse.TimeTagFlag = settings.TimeTagFlag;
                    TerminalSettings.Instanse.TimeTagData = settings.TimeTagData;
                    w.DialogResult = false;
                });
                return command;
            }
        }

        public RelayCommand<Window> TIOKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    try
                    {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.Append(int.Parse(TIList[0].DataValue).ToString("X2"));
                        stringBuilder.Append(int.Parse(TIList[1].DataValue).ToString("X4"));
                        TerminalSettings.Instanse.TimeTagData = stringBuilder.ToString();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> TICancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                   
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
